from flask import Blueprint, jsonify ,request

from service import libraryService
from service.libraryService import *

library_bp = Blueprint('library_bp', __name__)

#获取全部个人图书馆
@library_bp.route('/', methods=['GET'])
def getAllLibrary():
    return libraryService.getAllLibraryList()

#根据userID查询个人图书馆
@library_bp.route('/<int:userid>', methods=['GET'])
def getLibrary(userid):
    return libraryService.getLibraryByUserID(userid)

#添加个人图书馆
@library_bp.route('/',methods=['POST'])
def addLibrary():
    data = request.get_json()

    library = Library(data['library_name'], data['author'],data['information'],
                      data['picture'], data['num'],data['price'], data['user_id'], data['user_name'])

    return libraryService.addNewLibrary(library)

#更新个人图书馆信息
@library_bp.route('/<int:userid>',methods=['POST'])
def updateLibrary(userid):
    data = request.get_json()
    library = Library(data['library_name'], data['author'],data['information'],
                      data['picture'], data['num'],data['price'], data['user_id'], data['user_name'])

    return libraryService.updateLibrary(userid,library)

#删除个人图书馆
@library_bp.route('/delete',methods=['POST'])
def deleteLibrary():
    data = request.get_json()
    library_name=data['library_name']
    print(library_name)
    return libraryService.deleteLibrary(library_name)